<?php
// application/Bootstrap.php
/**
 * demo-zf application
 *
 * Bootstrap
 *
 * @author Adam Pilacki <adam.pilacki@gmail.com>
 */
final class Bootstrap
    extends Zend_Application_Bootstrap_Bootstrap
{
    /**
     * Resets array of run marks for all bootstraped resources
     * It means that after use of this method
     * every resource can be bootstraped again
     *
     * Use it wisely
     * e.g. use it before forwarding to other module
     */
    public final function makeAllResourcesBootstrapable()
    {
        $this->_run = array();
    }

    /**
     * After use this method resource can be bootstraped again
     *
     * @param string $resource
     */
    public final function makeResourceBootstrapable($resource)
    {
        if (in_array($resource, $this->_run)) {
            unset($this->_run[$resource]);
        }
    }

    protected function _initAutoload()
    {
        $autoloader = new Zend_Application_Module_Autoloader(array(
            'namespace' => '',
            'basePath'  => APPLICATION_PATH,
        ));

        /* add resources types */
        $autoloader->addResourceType('adamp', '/../vendor/AdamP', 'AdamP_');
        $autoloader->addResourceType('lib', '/lib', 'Lib_');
        return $autoloader;
    }

    /**
     * Set up final config and return it
     *
     * @return Zend_Config_Ini
     */
    protected function _initFinalConfig()
    {
        $moduleName = Zend_Registry::get('moduleName');

        $cache = Lib_Cache::getCacheForIni(240, array(APPLICATION_PATH . '/../configs/application.ini'), 'final');
        if (!$config = $cache->load($moduleName)) {
            $config = $this->_getConfig($moduleName);
            $cache->save($config);
        }

        $this->setOptions($config->toArray());

        Zend_Registry::set('config', $config);

        return $config;
    }

    /**
     * Prepares config and returns it
     *
     * @param string $moduleName
     * @return Zend_Config_Ini
     */
    private function _getConfig($moduleName)
    {
        $config = new Zend_Config($this->getOptions(), true);

        $configModule = new Zend_Config_Ini(
            APPLICATION_PATH . '/../configs/modules/' . $moduleName . '.ini',
            APPLICATION_ENV
            // TODO comment out code below and use as third argument if more options are needed
//            array(
//                'allowModifications' => true,
//            )
        );

        /* try to merge with local module config */
        try {
            $configModule->merge(new Zend_Config_Ini(APPLICATION_PATH . '/../configs/local/modules/' . $moduleName . '.ini', APPLICATION_ENV));
        } catch (Zend_Config_Exception $e) {
            /**
             * TODO coment code line below if using for real application
             */
            AdamP_Debug_Firebug::log('no local config for module ' . $moduleName, Zend_Log::WARN);
        }

        /* merge app config with module config */
        $config->merge($configModule);
        $config->setReadOnly();
        return $config;
    }
}